﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Timers;

// <summary>
/// Author: kiruthika.s
/// Created Date:22-Feb-2011
/// Description:Used to save the Common variables and functions.
/// </summary>
namespace Scheduler
{
    public partial class ClearLogscheduler : Form
    {
        int remainingDays = 0;

        /// <summary>
        /// Timer control 
        /// </summary>
        private System.Timers.Timer timerScheduler;
        private bool isStarted = false;

        /// <summary>
        /// Default constructor
        /// </summary>
        public ClearLogscheduler()
        {
            InitializeComponent();
        }
       
        /// <summary>
        /// Intiate timer
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClearLogscheduler_Load(object sender, EventArgs e)
        {
            remainingDays = 0;
            this.Hide();
            InitSchedular();
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            this.Visible = false;
            this.Update();
        }

        /// <summary>
        /// Used to disable the timer control
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            stop();
            timerScheduler.Dispose();
            timerScheduler = null;
        }

        /// <summary>
        /// Used to handle timer elapsed handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timerScheduler_Elapsed(object sender, EventArgs e)
        {
            if (remainingDays > 0)
            {
                timerScheduler.Interval = TimeSpan.FromHours(24).TotalMilliseconds;
                remainingDays--;
                return;
            }
            else
            {
                timerScheduler.Enabled = false;
                //Read Settings parameter "LastUpdatedDate" and "ScheduleType"  
                DateTime lastUpdateDate = EncircleFTM.clsSettings.lastClearedDate;
                int Day = EncircleFTM.clsSettings.lastClearDay;
                int Month = EncircleFTM.clsSettings.lastClearMonth;
                int Year = EncircleFTM.clsSettings.lastClearYear;
                int Hour = EncircleFTM.clsSettings.lastClearHour;
                int Min = EncircleFTM.clsSettings.lastClearMin;
                int Sec = EncircleFTM.clsSettings.lastClearSec;

                EncircleFTM.clsCommonData.ClearLogSchedule scheduleType = EncircleFTM.clsSettings.objClearLogSchedule;

                //calculate the scheduled time is reached or not by using method clsCommonData.SchedulerClearLog method
                EncircleFTM.clsCommonData.SchedulerStatus schedularStatus =
                    EncircleFTM.clsCommonData.SchedulerClearLog(scheduleType, Day, Month, Year, Hour, Min, Sec);

                //if isReached the shceduled time clear the log  (method:ClearLog)
                EncircleFTM.clsCommonData.ClearLog();

                //finally update the last clear log date using (method:clsSettings.UpdateClearLogDate)
                if (EncircleFTM.clsSettings.UpdateClearLogDate(DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year,
                    DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second))
                {
                    InitSchedular();
                }
                    //timerScheduler.Interval = schedularStatus.ElapsedInterval;
            }
        }
    
        /// <summary>
        /// Intializing the schedular and assign timer interval
        /// </summary>
        public void InitSchedular()
        {
            try
            {
                timerScheduler = new System.Timers.Timer();
                timerScheduler.Elapsed += new System.Timers.ElapsedEventHandler(timerScheduler_Elapsed);

                //Read Settings parameter "LastUpdatedDate" and "ScheduleType"  
                DateTime lastUpdateDate = EncircleFTM.clsSettings.lastClearedDate;
                EncircleFTM.clsCommonData.ClearLogSchedule scheduleType = EncircleFTM.clsSettings.objClearLogSchedule;
                int Day = EncircleFTM.clsSettings.lastClearDay;
                int Month = EncircleFTM.clsSettings.lastClearMonth;
                int Year = EncircleFTM.clsSettings.lastClearYear;
                int Hour = EncircleFTM.clsSettings.lastClearHour;
                int Min = EncircleFTM.clsSettings.lastClearMin;
                int Sec = EncircleFTM.clsSettings.lastClearSec;

                //calculate the scheduled time is reached or not by using method clsCommonData.SchedulerClearLog method
                EncircleFTM.clsCommonData.SchedulerStatus schedularStatus = EncircleFTM.clsCommonData.SchedulerClearLog(
                    scheduleType,Day,Month,Year, Hour,Min,Sec);

                double timerInterval = schedularStatus.ElapsedInterval;
                bool isCustomTimer = false;
                switch (scheduleType)
                {
                    case EncircleFTM.clsCommonData.ClearLogSchedule.EveryDay:
                        InitTimerInterval(schedularStatus);
                        remainingDays = 0;
                        break;
                    case EncircleFTM.clsCommonData.ClearLogSchedule.EveryWeek:
                        InitTimerInterval(schedularStatus);
                        remainingDays = 0;
                        break;
                    case EncircleFTM.clsCommonData.ClearLogSchedule.EveryMonth:
                        InitTimerInterval(schedularStatus);
                        isCustomTimer = true;
                        break;
                    case EncircleFTM.clsCommonData.ClearLogSchedule.EveryQuarter:
                        InitTimerInterval(schedularStatus);
                        isCustomTimer = true;
                        break;
                    case EncircleFTM.clsCommonData.ClearLogSchedule.EveryHalfyear:
                        InitTimerInterval(schedularStatus);
                        isCustomTimer = true;
                        break;
                    case EncircleFTM.clsCommonData.ClearLogSchedule.EveryYear:
                        InitTimerInterval(schedularStatus);
                        isCustomTimer = true;
                        break;
                    case EncircleFTM.clsCommonData.ClearLogSchedule.None:                      
                        break;
                }

                if (isCustomTimer)
                {
                    //Convert milliseconds to days
                    remainingDays = TimeSpan.FromMilliseconds(schedularStatus.ElapsedInterval).Days ;

                    if (remainingDays > 0)
                    {
                        timerScheduler.Interval = TimeSpan.FromHours(24).TotalMilliseconds;
                        timerInterval = timerScheduler.Interval;
                        remainingDays--;
                    }
                    else
                    {
                        //if isReached the shceduled time clear the log  (method:ClearLog)
                        EncircleFTM.clsCommonData.ClearLog();

                        //finally update the last clear log date using (method:clsSettings.UpdateClearLogDate)
                        if (EncircleFTM.clsSettings.UpdateClearLogDate(DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second))
                        {
                            timerScheduler.Interval = TimeSpan.FromHours(24).TotalMilliseconds;
                            timerInterval = timerScheduler.Interval;
                        }
                    }
                }


                if (timerInterval > 1)
                    Start();
                else
                    stop();
            }
            catch (Exception ex)
            {
                //write to error log
                EncircleFTM.clsCommonData.WriteError(ex.Message);
            }
        }

        /// <summary>
        /// Used to initializing the timer 
        /// </summary>
        /// <param name="schedularStatus"></param>
        public void InitTimerInterval(EncircleFTM.clsCommonData.SchedulerStatus schedularStatus)
        {
            
            if (schedularStatus.isReached)
            {
                //if isReached the shceduled time clear the log  (method:ClearLog)
                EncircleFTM.clsCommonData.ClearLog();

                //finally update the last clear log date using (method:clsSettings.UpdateClearLogDate)
                if (EncircleFTM.clsSettings.UpdateClearLogDate(DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second))
                    timerScheduler.Interval = schedularStatus.ElapsedInterval;
            }
            else
            {
                //the interval calculation will be return by the method SchedulerClearLog
                if (schedularStatus.ElapsedInterval > 1)
                    timerScheduler.Interval = schedularStatus.ElapsedInterval;
                else
                    timerScheduler.Interval = 1;
            }
        }

        /// <summary>
        /// Used to start the timer control
        /// </summary>
        public void Start()
        {
            timerScheduler.Enabled = true;
            isStarted = true;
        }

        /// <summary>
        /// Used to stop the timer control
        /// </summary>
        public void stop()
        {
            timerScheduler.Enabled = false;
            isStarted = false;
        }
    }
}
